﻿// Copyright (c) zhenlei520 All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text;
using EInfrastructure.Core.Words.Extension.ImportDict.Common.Enum;

namespace EInfrastructure.Core.Words.Extension.ImportDict.Common.Helpers
{
    internal static class CollectionHelper
    {
        public static string GetString(IEnumerable<string> list, string split, BuildTypeEnum buildType)
        {
            var sb = new StringBuilder();


            if (list == null)
            {
                return "";
            }

            foreach (string s in list)
            {
                sb.Append(s + split);
            }
            if (sb.Length == 0)
            {
                return "";
            }
            if (buildType == BuildTypeEnum.RightContain)
            {
                return sb.ToString();
            }
            if (buildType == BuildTypeEnum.FullContain)
            {
                return split + sb;
            }
            string str = sb.ToString();
            if (split.Length > 0)
            {
                str = str.Remove(sb.Length - 1);
            }
            if (buildType == BuildTypeEnum.None)
            {
                return str;
            }
            return split + str;
        }

        /// <summary>
        /// Cartesian product
        /// </summary>
        /// <param name="list"></param>
        /// <param name="count"></param>
        /// <param name="result"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private static string Descartes(IList<IList<string>> list, int count, IList<string> result, string data)
        {
            string temp = data;
            //获取当前数组
            IList<string> astr = list[count];
            //循环当前数组
            foreach (string item in astr)
            {
                if (count + 1 < list.Count)
                {
                    temp += Descartes(list, count + 1, result, data + item);
                }
                else
                {
                    result.Add(data + item);
                }
            }
            return temp;
        }

        /// <summary>
        ///     多音字情况下，做笛卡尔积
        /// </summary>
        /// <param name="codes"></param>
        /// <returns></returns>
        public static IList<string> Descartes(IList<IList<string>> codes)
        {
            var result = new List<string>();
            Descartes(codes, 0, result, string.Empty);
            return result;
        }

        /// <summary>
        ///     只取每个字的第一个编码，返回这些编码的List
        /// </summary>
        /// <param name="codes"></param>
        /// <returns></returns>
        public static IList<string> DescarteIndex1(IList<IList<string>> codes)
        {
            var result = new List<string>();
            foreach (var code in codes)
            {
                result.Add(code[0]);
            }
            return result;
        }

        public static IList<string> CartesianProduct(IList<IList<string>> codes, String split)
        {
            int count = 1;
            foreach (var code in codes)
            {
                count *= code.Count;
            }
            var result = new List<string>();
            for (int i = 0; i < count; i++)
            {
                var line = new string[codes.Count];
                for (int j = 0; j < codes.Count; j++)
                {
                    line[j] = codes[j][i%codes[j].Count];
                }
                result.Add(String.Join(split, line));
            }
            return result;
        }

        public static IList<string> CartesianProduct(IList<IList<string>> codes, String split, BuildTypeEnum buildType)
        {
            IList<string> list = CartesianProduct(codes, split);
            if (buildType == BuildTypeEnum.None) return list;
            var result = new List<string>();
            foreach (string line in list)
            {
                string newline = line;
                if (buildType == BuildTypeEnum.FullContain || buildType == BuildTypeEnum.LeftContain)
                {
                    newline = split + newline;
                }
                if (buildType == BuildTypeEnum.FullContain || buildType == BuildTypeEnum.RightContain)
                {
                    newline = newline + split;
                }
                result.Add(newline);
            }
            return result;
        }
    }
}